var start = 0;
var limit = 20;

$(document).ready(function() {
	

	var id = parseInt(window.location.hash.substring(1));
	if (!isNaN(id)) {
		$("tr#" + id + " td").effect("highlight", {color:'#00FF00'}, 2000);
		window.location.hash = "";
	}
	
	$("#next").click(function() {
		window.location.hash = "";
		initPagination();
		window.location.href = $("a#next").attr('href') + (start+limit) + '/' + limit; 
		return false;
	});

	$("#previous").click(function() {
		window.location.hash = "";
		initPagination();
		window.location.href = $("a#previous").attr('href') + (start-limit) + '/' + limit; 
		return false;
	});
	
	$("input#all").click(function() {
		
		if ($(this).attr('checked') == 'checked') {
			$("input[name=id]").each(function() {
				$(this).attr('checked', true);
			});
		} else {
			$("input[name=id]").each(function() {
				$(this).attr('checked', false);
			});
		}
	});
	
	$("#multipleDeletes").click(function(){
		var letters = [];
		$('input:checked[name=id]').each(function() {
		  letters.push($(this).attr('id'));
		});
		if (confirm("Etes-vous sûr ?")) {
			$.fancybox.showActivity();
			req = { 'ids' : letters};
			$.post($("a#multipleDeletes").attr('href'), req, function(data) {
		        try {
		        	resp = eval(data);
		        }
		        catch(err) {
		        	$.fancybox.hideActivity();
		        	alert("Une erreur est survenue.");
		        	$(document).html(data);
		        	return false;
		        }
		        if (resp.status == "success") {
		        	$.fancybox.hideActivity();
		        	for (var id in letters) {
		        		console.log(letters[id]);
		        		var tr = $("tr#" + letters[id]);
		        		$("tr#" + letters[id] + " td").effect("highlight", {color:'red'}, 3000);
		        		$(tr).delay(300).fadeOut(300, function() {
		        			$(tr).remove();                        
		        		});
		        	}
	            } else {
	            	$.fancybox.hideActivity();
	            }
			}, 'json');
		}
		return false;
	});
	
	$("a.delete").click(function() {
		if (confirm("Etes-vous sûr ?")) {
			$.fancybox.showActivity();
			$.getJSON($(this).attr('href'), function(data) {
		        try {
		        	resp = eval(data);
		        }
		        catch(err) {
		        	$.fancybox.hideActivity();
		        	alert("Une erreur inconnue est survenue.");
		        	$(document).html(data);
		        	return false;
		        }
		        if (resp.status == "success") {
		        	$.fancybox.hideActivity();
                    var tr = $("tr#" + resp.id);
                    $("tr#" + resp.id + " td").effect("highlight", {color:'red'}, 3000);
                    $(tr).delay(300).fadeOut(300, function() {
                        $(tr).remove();                        
                    });
	            } else {
	            	$.fancybox.hideActivity();
	            }
			});
			return false;
		}
		else {
			return false;
		}
	});
});

function initPagination() {
	var pos = window.location.href.lastIndexOf('/');
	console.log("pos="+pos);
	var sub = window.location.href.substring(pos+1);
	console.log("sub="+sub);
	var tLimit = parseInt(sub);
	console.log("tlimit="+tLimit);
	if (isNaN(tLimit)) {
		limit = 20;
		start = 0;
		console.log("limit="+limit);
		return;
	} else {
		limit = tLimit;
	}
	console.log("limit="+limit);
	var shortenHref = window.location.href.substring(0, window.location.href.length - tLimit.toString().length - 1);
	console.log("shortenHref="+shortenHref);
	pos = shortenHref.lastIndexOf('/');
	console.log("pos="+pos);
	var tStart = parseInt(shortenHref.substring(pos+1));
	console.log("tStart="+tStart);
	if (isNaN(tLimit)) {
		start = 0;
	} else {
		start = tStart;
	}
	console.log("start="+start);
}